package com.vincce.sport.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.entity.Advert;
import com.vincce.sport.entity.City;
import com.vincce.sport.entity.CompetitionSeason;
import com.vincce.sport.entity.Page;
import com.vincce.sport.entity.Player;
import com.vincce.sport.entity.PlayerApi;
import com.vincce.sport.entity.PlayerDataApi;
import com.vincce.sport.entity.PlayerDataTask;
import com.vincce.sport.entity.PlayerDate;
import com.vincce.sport.entity.School;
import com.vincce.sport.entity.Team;
import com.vincce.sport.entity.TeamApi;
import com.vincce.sport.entity.Zone;
import com.vincce.sport.mapper.AdvertMapper;
import com.vincce.sport.mapper.CityMapper;
import com.vincce.sport.mapper.CompetitionSeasonMapper;
import com.vincce.sport.mapper.PlayerDataTaskMapper;
import com.vincce.sport.mapper.PlayerDateMapper;
import com.vincce.sport.mapper.PlayerMapper;
import com.vincce.sport.mapper.SchoolMapper;
import com.vincce.sport.mapper.TeamMapper;
import com.vincce.sport.mapper.ZoneMapper;


@Controller
@RequestMapping("/playerDate")
public class PlayerDateController extends BaseController{

	@Resource
	private PlayerDateMapper playerDateMapper;
	@Resource
	private PlayerMapper playerMapper;
	@Resource
	private CompetitionSeasonMapper competitionSeasonMapper;
	@Resource
	private CityMapper cityMapper;
	@Resource
	private SchoolMapper schoolMapper;
	@Resource
	private TeamMapper teamMapper;
	@Resource
	private ZoneMapper zoneMapper;
	@Resource
	private PlayerDataTaskMapper playerDataTaskMapper;
	
	@Resource
	private AdvertMapper advertMapper;
	
	/**
	 * 图片路径替换
	 * @param imgStorageList
	 * @return
	 */
	public List<PlayerDataTask> replace(List<PlayerDataTask> imgTempList){
		if(imgTempList!=null && imgTempList.size()>0){
			for(PlayerDataTask itl:imgTempList){
				itl.setUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getUrl(), "ori"));
			}
		}
		return imgTempList;
	}
	
	
	/**
	 * 根据球员Id查询球员赛季数据
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/personalDateSeasonList")
	public String personalDateSeasonList(Model model,Integer id){
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("playerId", id);
		List<PlayerDate> playerDates = playerDateMapper.queryPalyerDateSeason(map);
		Player player =playerMapper.queryPalyerDate(id);
		
		player.setUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+player.getUrl(), "ori"));
		PlayerDate pDate = playerDateMapper.querySeasonName(map);
		/*for (int i = 0; i < playerDates.size(); i++) {
			//罚球命中率
			String freeThrowHitRate = playerDates.get(i).getFreeThrowHitRate()+"%";
			playerDates.get(i).setFreeThrowHitRate(freeThrowHitRate);
			//2分命中率
			String twoPointsHitRate=playerDates.get(i).getTwoPointsHitRate()+"%";
			playerDates.get(i).setTwoPointsHitRate(twoPointsHitRate);
			//3分命中率
			String threePointsHitRate=playerDates.get(i).getThreePointsHitRate()+"%";
			playerDates.get(i).setThreePointsHitRate(threePointsHitRate);
			
			
		}*/
		
		for (int i = 0; i <playerDates.size(); i++) {
			float freeRate=playerDates.get(i).getFreeThrowHitRate()*100;
			playerDates.get(i).setFreeThrowHitRate(Float.parseFloat(String.format("%.1f", freeRate)));
			
			float twoRate=playerDates.get(i).getTwoPointsHitRate()*100;
			playerDates.get(i).setTwoPointsHitRate(Float.parseFloat(String.format("%.1f", twoRate)));
			
			float thrreRate=playerDates.get(i).getThreePointsHitRate()*100;
			playerDates.get(i).setThreePointsHitRate(Float.parseFloat(String.format("%.1f", thrreRate)));
			
			float totalHit=playerDates.get(i).getTotalHit()*100;
			playerDates.get(i).setTotalHit(Float.parseFloat(String.format("%.1f", totalHit)));
		}
		
		Page<Advert> footerAdvertPage=new Page<Advert>();
		Map<String, Object> filter = new HashMap<String, Object>();
		filter=new HashMap<String, Object>();	
	    filter.put("name", "个人赛季数据");
	    filter.put("status", 1);
	    
	    footerAdvertPage.setLimit(1);
	    footerAdvertPage.setFilter(filter);
	    footerAdvertPage.setResult(replace1(advertMapper.queryAdverListPage(footerAdvertPage)));
	    model.addAttribute("footerAdvertPage",footerAdvertPage);
		model.addAttribute("id",id);
		model.addAttribute("pDate",pDate);
		model.addAttribute("playerDates",playerDates);
		model.addAttribute("player",player);
		return "/foreground/personalDateSeason";
	}
	
	/**
	 * 个人数据，历史数据查询
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/personalDateHistoryList")
	public String personalDateHistoryList(Model model,Integer id,Integer seasonId){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("playerId", id);
		map.put("seasonId", seasonId);
		List<CompetitionSeason> competitionSeasonList=competitionSeasonMapper.querySeasonByAll();
		
		List<CompetitionSeason> competitionSeasonList2=competitionSeasonMapper.queryCompetitionSeasonById(map);
		if (competitionSeasonList2.size()>0) {
			model.addAttribute("seasonName",competitionSeasonList2.get(0).getName());
		}	
		List<PlayerDate> playerDates = playerDateMapper.queryPalyerDateSeason(map);
		for (int i = 0; i <playerDates.size(); i++) {
			float freeRate=playerDates.get(i).getFreeThrowHitRate()*100;
			playerDates.get(i).setFreeThrowHitRate(Float.parseFloat(String.format("%.1f", freeRate)));
			
			float twoRate=playerDates.get(i).getTwoPointsHitRate()*100;
			playerDates.get(i).setTwoPointsHitRate(Float.parseFloat(String.format("%.1f", twoRate)));
			
			float thrreRate=playerDates.get(i).getThreePointsHitRate()*100;
			playerDates.get(i).setThreePointsHitRate(Float.parseFloat(String.format("%.1f", thrreRate)));
			
			float totalHit=playerDates.get(i).getTotalHit()*100;
			playerDates.get(i).setTotalHit(Float.parseFloat(String.format("%.1f", totalHit)));
		}
		Player player =playerMapper.queryPalyerDate(id);
		player.setUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+player.getUrl(), "ori"));

		PlayerDate pDate = playerDateMapper.querySeasonName(map);
		/*for (int i = 0; i < playerDates.size(); i++) {
			//罚球命中率
			String freeThrowHitRate = playerDates.get(i).getFreeThrowHitRate()+"%";
			playerDates.get(i).setFreeThrowHitRate(freeThrowHitRate);
			//2分命中率
			String twoPointsHitRate=playerDates.get(i).getTwoPointsHitRate()+"%";
			playerDates.get(i).setTwoPointsHitRate(twoPointsHitRate);
			//3分命中率
			String threePointsHitRate=playerDates.get(i).getThreePointsHitRate()+"%";
			playerDates.get(i).setThreePointsHitRate(threePointsHitRate);
			
			
		}*/
		
		if (playerDates.size()>0) {
			model.addAttribute("pDate",playerDates.get(0).getSeasonName());
		}
		Page<Advert> footerAdvertPage=new Page<Advert>();
		Map<String, Object> filter = new HashMap<String, Object>();
		filter=new HashMap<String, Object>();	
	    filter.put("name", "个人历史数据");
	    filter.put("status", 1);
	    
	    footerAdvertPage.setLimit(1);
	    footerAdvertPage.setFilter(filter);
	    footerAdvertPage.setResult(replace1(advertMapper.queryAdverListPage(footerAdvertPage)));
	    model.addAttribute("footerAdvertPage",footerAdvertPage);
		
		model.addAttribute("id",id);
		model.addAttribute("player",player);
		//model.addAttribute("pDate",pDate);
		model.addAttribute("competitionSeasonList",competitionSeasonList);
		model.addAttribute("playerDates",playerDates);
		return "/foreground/personalDateHistory";
		
	}
	
	
	@RequestMapping("/personalDateApi")
	@ResponseBody
	public String personalDateApi(Integer id,Integer seasonId){
		Map<String, Object> result=new HashMap<String, Object>();
		Map<String, Object> map=new HashMap<String, Object>();
		if (id==null || seasonId==null) {
			result.put("suc", false);
			result.put("msg", "球员ID和赛季ID不能为空");
		}
		
		else {
			map.put("playerName", id);
			map.put("seasonId", seasonId);
			List<PlayerDataApi> playerDates = playerDateMapper.queryPalyerDateApi(map);
			result.put("list", playerDates);
			result.put("suc", true);
			result.put("msg", "成功");
			
		}
		
		return JSON.toJSONString(result);
		
		
	}
	
	
	@RequestMapping("/personalDateByNameApi")
	@ResponseBody
	public String personalDateByNameApi(String playerName,Integer seasonId){
		Map<String, Object> result=new HashMap<String, Object>();
		Map<String, Object> map=new HashMap<String, Object>();
		if (playerName==null || seasonId==null) {
			result.put("suc", false);
			result.put("msg", "球员名称和赛季ID不能为空");
		}
		
		else {
			map.put("playerName", playerName);
			map.put("seasonId", seasonId);
			List<PlayerDataApi> playerDates = playerDateMapper.queryPalyerDateApi(map);
			result.put("list", playerDates);
			result.put("suc", true);
			result.put("msg", "成功");
			
		}
		
		return JSON.toJSONString(result);
		
		
	}
	
	/**
	 * 球员数据API 
	 * @param schoolName
	 * @param teamName
	 * @param contestId
	 * @return
	 */
	@RequestMapping("/getPlayerDataApi")
	@ResponseBody
	public String getPlayerDataApi(String schoolName,String teamName,Integer contestId){
		
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> result=new HashMap<String, Object>();
		if (schoolName==null || schoolName==null || contestId==null) {
			result.put("suc", false);
			result.put("msg", "学校名称球队名称和赛程ID不能为空");
		}
		else{
			map.put("schoolName", schoolName);
			map.put("teamName", teamName);
			map.put("contestId", contestId);
			
			List<PlayerApi> apis=playerDateMapper.queryPlayerApi(map);
			result.put("list", apis);
			result.put("suc", true);
			result.put("msg", "成功");
		}
		
		return JSON.toJSONString(result);
	}
	
	/**
	 * 根据球队名称，赛程和赛季查询球队下面的所有球员
	 * @param contestId
	 * @param seasonId
	 * @param teamName
	 * @return
	 */

	@RequestMapping("/playerDateByTeamApi")
	@ResponseBody
	public String playerDateByTeamApi(Integer contestId,Integer seasonId,String teamName){
		Map<String, Object> result=new HashMap<String, Object>();
		Map<String, Object> map=new HashMap<String, Object>();
		if (teamName==null || seasonId==null || contestId==null) {
			result.put("suc", false);
			result.put("msg", "赛程ID赛季ID和球队名称不能为空");
		}
		
		else {
			map.put("teamName", teamName);
			map.put("seasonId", seasonId);
			map.put("contestId", contestId);
			List<PlayerApi> playerDates = playerDateMapper.queryPlayerByTeamApi(map);
			result.put("list", playerDates);
			result.put("suc", true);
			result.put("msg", "成功");
			
		}
		
		return JSON.toJSONString(result);
	}
	
	
	
	/**
	 * 按照学校名称和球队名称查询数据
	 * @param teamName
	 * @param schoolName
	 * @return
	 */
	@RequestMapping("/queryPlayerByStatuApi")
	@ResponseBody
	public String queryPlayerByStatuApi(String teamName,String schoolName){
		Map<String, Object> result=new HashMap<String, Object>();
		Map<String, Object> map=new HashMap<String, Object>();
		if (teamName==null || schoolName==null) {
			result.put("suc", false);
			result.put("msg", "学校名称和球队名称不能为空");
		}
		
		else {
			map.put("teamName", teamName);
			map.put("seasonId", schoolName);
			List<PlayerApi> playerDates = playerDateMapper.queryPlayerByStatuApi(map);
			if(playerDates != null && playerDates.size() > 0){
				result.put("list", playerDates);
				result.put("msg", "成功");
			}else{
				result.put("msg", "暂无数据");
			}
			result.put("suc", true);
			
		}
		
		return JSON.toJSONString(result);
	}
	
	
	
	
	/**
	 * 个人数据
	 * @param model
	 * @param zone
	 * @param city
	 * @param school
	 * @param team
	 * @param player
	 * @return
	 */
	@RequestMapping("/personalDataList")
	public String personalDataList(Model model,Integer teamId,String schoolId,String cityId,String zoneId,String seasonId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("teamId", teamId);
		map.put("schoolId", schoolId);
		map.put("cityId", cityId);
		map.put("zoneId", zoneId);
		map.put("seasonId", seasonId);
		List<CompetitionSeason> seasons= competitionSeasonMapper.querySeasonByAll();
		List<Zone> zones=zoneMapper.queryByZoneAll();
		List<PlayerDataTask> playerList=playerDataTaskMapper.queryPlayerDataById(map);
		for (int i = 0; i < playerList.size(); i++) {
			float totalHit=playerList.get(i).getTotalHit()*100;
			playerList.get(i).setTotalHit(Float.parseFloat(String.format("%.1f", totalHit)));
		}
		replace(playerList);
		model.addAttribute("seasons",seasons);
		model.addAttribute("zones",zones);
		model.addAttribute("playerList",playerList);
		
		model.addAttribute("teamId",teamId);
		model.addAttribute("schoolId",schoolId);
		model.addAttribute("cityId",cityId);
		model.addAttribute("zoneId",zoneId);
		model.addAttribute("seasonId",seasonId);
		return "/foreground/personalDataList";
	}
	
	
	@RequestMapping("/queryCityByZoneId")
	@ResponseBody
	public String queryCityByZoneId(Integer zoneId){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("zoneId", zoneId);
		List<City> list= cityMapper.queryCityByZoneId(map); 
		return JSON.toJSONString(list);
		
	}
	
	@RequestMapping("/querySchoolByCityId")
	@ResponseBody
	public String querySchoolByCityId(Integer cityId){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("cityId", cityId);
		List<School> schoolList=schoolMapper.queryByCityId(map);
		return JSON.toJSONString(schoolList);
	}
	
	@RequestMapping("/querySchoolByMap")
	@ResponseBody
	public String querySchoolByMap(Integer zoneId){
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("zoneId", zoneId);
		List<School> schools =schoolMapper.querySchoolByMap(map);
		return JSON.toJSONString(schools);
	}
	
	
	@RequestMapping("/queryTeamBySchoolId")
	@ResponseBody
	public String queryTeamBySchoolId(Integer schoolId){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("schoolId", schoolId);
		List<Team> teamList = teamMapper.queryTeamBySchoolId(map);
		return JSON.toJSONString(teamList);
	}
	@RequestMapping("/test")
	@ResponseBody
	public String test(){
		List<PlayerDataTask> dataTasks = playerDataTaskMapper.queryByPlayerDataTaskList();
		return JSON.toJSONString(dataTasks);
	}
	
	/**
	 * 图片路径替换
	 * @param imgStorageList
	 * @return
	 */
	public List<Advert> replace1(List<Advert> advertList){
		if(advertList!=null && advertList.size()>0){
			for(Advert adv:advertList){
				adv.setAdvertImg(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+adv.getAdvertImg(), "ori"));
			}
		}
		return advertList;
	}

}
